<?php
/**
 * Created by PhpStorm.
 * User: jacklove
 * Date: 2019/5/17
 * Time: 15:04
 */

namespace app\api\controller;

use think\Db;
use think\Request;
use think\Controller;

class Nocommon extends Controller
{
    public function _initialize()
    {
        $post = Request::instance()->post();


     }


    /**
     * 接口返回封装
     */
    protected function apiReturn($code='-1',$msg,$data=[]){
        $code = (string)$code;
        if($code == '-1'){
            exit('参数调用错误');
        }
        if(empty($data)){
            if($code=='200'){
                exit(json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data)));
            }else{
                exit(json_encode(array('code'=>$code,'msg'=>$msg)));
            }
        }else{
            exit(json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data)));
        }
    }
    
    /*
     * 接口分页类
     */
    protected function p()
    {
        //客户端传过来的分页数字
        $p = input('page',1,'intval');
        if($p < 1){
            $p = 1;
        }
        return $p;
    }

    function deep_in_array($value, $array) {
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) {
                    return true;
                } else {
                    continue;
                }
            }
            if(in_array($value, $item)) {
                return true;
            } else if($this->deep_in_array($value, $item)) {
                return true;
            }
        }
        return false;
    }

//处理时间
    function time_ago($posttime){
        //当前时间的时间戳
        $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
        //之前时间参数的时间戳
        $posttimes = strtotime($posttime);

        //相差时间戳
        $counttime = $nowtimes - $posttimes;

        //进行时间转换
        if($counttime<=60){

            return '刚刚';

        }else if($counttime>60 && $counttime<3600){

            return intval(($counttime/60)).'分钟前';

        }else if($counttime>=3600 && $counttime<3600*24){

            return intval(($counttime/3600)).'小时前';

        }else if($counttime>=3600*24 && $counttime<3600*24*2){

            return '昨天';

        }else if($counttime>=3600*24*2 && $counttime<3600*24*3){

            return '前天';

        }else if($counttime>=3600*24*3 && $counttime<=3600*24*30){

            return intval(($counttime/(3600*24))).'天前';

        }else{

            return $posttime;

        }
    }
}